﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:Task
    /// </summary>
    public partial class Task
    {
        /// <summary>
        /// 重点督察（文件阅办）
        /// </summary>
        public Task()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="FTaskID">编号</param>
        /// <returns>是否存在该记录</returns>
        public bool Exists(decimal FTaskID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_Task");
            strSql.Append(" where FTaskID=@FTaskID");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskID", SqlDbType.Decimal)
			};
            parameters[0].Value = FTaskID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">增加实体类</param>
        /// <returns>成功条数</returns>
        public decimal Add(ElingOA6_Admin_DSJCJ.Model.Task model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_Task(");
            strSql.Append("FDeptNo,FBeginDate,FEndDate,FScore,FContent,FTaskClassCd,FEntityNo,FInsEmpNo,FInsDate,FInsIP,FUpdEmpNo,FUpdDate,FUpdIP,FCheckEmpNo,FCheckDate,FCheckIP,FStatus)");
            strSql.Append(" values (");
            strSql.Append("@FDeptNo,@FBeginDate,@FEndDate,@FScore,@FContent,@FTaskClassCd,@FEntityNo,@FInsEmpNo,@FInsDate,@FInsIP,@FUpdEmpNo,@FUpdDate,@FUpdIP,@FCheckEmpNo,@FCheckDate,@FCheckIP,@FStatus)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FBeginDate", SqlDbType.DateTime),
					new SqlParameter("@FEndDate", SqlDbType.DateTime),
					new SqlParameter("@FScore", SqlDbType.Decimal,9),
					new SqlParameter("@FContent", SqlDbType.NVarChar,-1),
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FCheckEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FCheckDate", SqlDbType.DateTime),
					new SqlParameter("@FCheckIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,20)};
            parameters[0].Value = model.FDeptNo;
            parameters[1].Value = model.FBeginDate;
            parameters[2].Value = model.FEndDate;
            parameters[3].Value = model.FScore;
            parameters[4].Value = model.FContent;
            parameters[5].Value = model.FTaskClassCd;
            parameters[6].Value = model.FEntityNo;
            parameters[7].Value = model.FInsEmpNo;
            parameters[8].Value = model.FInsDate;
            parameters[9].Value = model.FInsIP;
            parameters[10].Value = model.FUpdEmpNo;
            parameters[11].Value = model.FUpdDate;
            parameters[12].Value = model.FUpdIP;
            parameters[13].Value = model.FCheckEmpNo;
            parameters[14].Value = model.FCheckDate;
            parameters[15].Value = model.FCheckIP;
            parameters[16].Value = model.FStatus;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }
        
        /// <summary>
        ///  更新一条数据
        /// </summary>
        /// <param name="model">更新视图类</param>
        /// <returns>是否更新成功</returns>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.Task model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_Task set ");
            strSql.Append("FBeginDate=@FBeginDate,");
            strSql.Append("FEndDate=@FEndDate,");
            strSql.Append("FScore=@FScore,");
            strSql.Append("FContent=@FContent,");
            strSql.Append("FInsDate=@FInsDate,");
            strSql.Append("FInsIP=@FInsIP,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdIP=@FUpdIP,");
            strSql.Append("FCheckDate=@FCheckDate,");
            strSql.Append("FCheckIP=@FCheckIP,");
            strSql.Append("FStatus=@FStatus");
            strSql.Append(" where FTaskID=@FTaskID");
            SqlParameter[] parameters = {
					new SqlParameter("@FBeginDate", SqlDbType.DateTime),
					new SqlParameter("@FEndDate", SqlDbType.DateTime),
					new SqlParameter("@FScore", SqlDbType.Decimal,9),
					new SqlParameter("@FContent", SqlDbType.NVarChar,-1),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FCheckDate", SqlDbType.DateTime),
					new SqlParameter("@FCheckIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,20),
					new SqlParameter("@FTaskID", SqlDbType.Decimal,13),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FCheckEmpNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FBeginDate;
            parameters[1].Value = model.FEndDate;
            parameters[2].Value = model.FScore;
            parameters[3].Value = model.FContent;
            parameters[4].Value = model.FInsDate;
            parameters[5].Value = model.FInsIP;
            parameters[6].Value = model.FUpdDate;
            parameters[7].Value = model.FUpdIP;
            parameters[8].Value = model.FCheckDate;
            parameters[9].Value = model.FCheckIP;
            parameters[10].Value = model.FStatus;
            parameters[11].Value = model.FTaskID;
            parameters[12].Value = model.FDeptNo;
            parameters[13].Value = model.FTaskClassCd;
            parameters[14].Value = model.FEntityNo;
            parameters[15].Value = model.FInsEmpNo;
            parameters[16].Value = model.FUpdEmpNo;
            parameters[17].Value = model.FCheckEmpNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="FTaskID">编号</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(decimal FTaskID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_Task ");
            strSql.Append(" where FTaskID=@FTaskID");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskID", SqlDbType.Decimal)
			};
            parameters[0].Value = FTaskID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
      
        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="FTaskIDlist">编号集合以“，”分隔</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteList(string FTaskIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_Task ");
            strSql.Append(" where FTaskID in (" + FTaskIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="FTaskID">编号</param>
        /// <returns>实体对象</returns>
        public ElingOA6_Admin_DSJCJ.Model.Task GetModel(decimal FTaskID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FTaskClassName,FDeptName,FTaskID,FDeptNo,FBeginDate,FEndDate,FScore,FContent,FTaskClassCd,FEntityNo,FInsEmpNo,FInsDate,FInsIP,FUpdEmpNo,FUpdDate,FUpdIP,FCheckEmpNo,FCheckDate,FCheckIP,FStatus from v_Task ");
            strSql.Append(" where FTaskID=@FTaskID");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskID", SqlDbType.Decimal)
			};
            parameters[0].Value = FTaskID;

            ElingOA6_Admin_DSJCJ.Model.Task model = new ElingOA6_Admin_DSJCJ.Model.Task();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }



        /// <summary>
        /// DataRow转换model
        /// </summary>
        /// <param name="row">DataRow</param>
        /// <returns>实体对象</returns>
        public ElingOA6_Admin_DSJCJ.Model.Task DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.Task model = new ElingOA6_Admin_DSJCJ.Model.Task();
            if (row != null)
            {
                if (row["FTaskID"] != null && row["FTaskID"].ToString() != "")
                {
                    model.FTaskID = decimal.Parse(row["FTaskID"].ToString());
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row["FBeginDate"] != null && row["FBeginDate"].ToString() != "")
                {
                    model.FBeginDate = DateTime.Parse(row["FBeginDate"].ToString());
                }
                if (row["FEndDate"] != null && row["FEndDate"].ToString() != "")
                {
                    model.FEndDate = DateTime.Parse(row["FEndDate"].ToString());
                }
                if (row["FScore"] != null && row["FScore"].ToString() != "")
                {
                    model.FScore =  decimal.Parse(row["FScore"].ToString());
                }
                if (row["FContent"] != null)
                {
                    model.FContent = row["FContent"].ToString();
                }
                if (row["FTaskClassCd"] != null)
                {
                    model.FTaskClassCd = row["FTaskClassCd"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FInsEmpNo"] != null)
                {
                    model.FInsEmpNo = row["FInsEmpNo"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
                if (row["FUpdEmpNo"] != null)
                {
                    model.FUpdEmpNo = row["FUpdEmpNo"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
                if (row["FCheckEmpNo"] != null)
                {
                    model.FCheckEmpNo = row["FCheckEmpNo"].ToString();
                }
                if (row["FCheckDate"] != null && row["FCheckDate"].ToString() != "")
                {
                    model.FCheckDate = DateTime.Parse(row["FCheckDate"].ToString());
                }
                if (row["FCheckIP"] != null)
                {
                    model.FCheckIP = row["FCheckIP"].ToString();
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }

                if (row.Table.Columns.Contains("FTaskClassName"))
                {
                    if (row["FTaskClassName"] != null)
                    {
                        model.FTaskClassName = row["FTaskClassName"].ToString();
                    }
                }

                if (row.Table.Columns.Contains("FDeptName"))
                {
                    if (row["FDeptName"] != null)
                    {
                        model.FDeptName = row["FDeptName"].ToString();
                    }
                }
            }
            return model;
        }

       /// <summary>
        ///  获得数据列表
       /// </summary>
       /// <param name="strWhere">查询条件</param>
       /// <returns>Dataset对象</returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FTaskID,FDeptNo,FBeginDate,FEndDate,FScore,FContent,FTaskClassCd,FEntityNo,FInsEmpNo,FInsDate,FInsIP,FUpdEmpNo,FUpdDate,FUpdIP,FCheckEmpNo,FCheckDate,FCheckIP,FStatus ");
            strSql.Append(" FROM dbo.t_Task ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        /// <param name="Top">前几行</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序方式</param>
        /// <returns>Dataset对象</returns>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FTaskID,FDeptNo,FBeginDate,FEndDate,FScore,FContent,FTaskClassCd,FEntityNo,FInsEmpNo,FInsDate,FInsIP,FUpdEmpNo,FUpdDate,FUpdIP,FCheckEmpNo,FCheckDate,FCheckIP,FStatus ");
            strSql.Append(" FROM dbo.t_Task ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns>总记录数</returns>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM dbo.t_Task ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_Task";
            parameters[1].Value = "FTaskID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

